﻿<!DOCTYPE html>
<HTML>
<HEAD>
  <TITLE> ZTREE DEMO - beforeDrag / onDrag / beforeDrop / onDrop</TITLE>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
  <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
  <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>
  <SCRIPT type="text/javascript">
    <!--
    var setting = {
      edit: {
        drag: {
          autoExpandTrigger: true,
          prev: dropPrev,
          inner: dropInner,
          next: dropNext
        },
        enable: true,
        showRemoveBtn: false,
        showRenameBtn: false
      },
      data: {
        simpleData: {
          enable: true
        }
      },
      callback: {
        beforeDrag: beforeDrag,
        beforeDrop: beforeDrop,
        beforeDragOpen: beforeDragOpen,
        onDrag: onDrag,
        onDrop: onDrop,
        onExpand: onExpand
      }
    };

    var zNodes = [
      { id: 1, pId: 0, name: "can drag 1", open: true },
      { id: 11, pId: 1, name: "can drag 1-1" },
      { id: 12, pId: 1, name: "can drag 1-2" },
      { id: 121, pId: 12, name: "can drag 1-2-1" },
      { id: 122, pId: 12, name: "can drag 1-2-2" },
      { id: 123, pId: 12, name: "can drag 1-2-3" },
      { id: 13, pId: 1, name: "can't drag 1-3", open: true, drag: false },
      { id: 131, pId: 13, name: "can't drag 1-3-1", drag: false },
      { id: 132, pId: 13, name: "can't drag 1-3-2", drag: false },
      { id: 132, pId: 13, name: "can't drag 1-3-3", drag: false },
      { id: 2, pId: 0, name: "can't drag my child out 2", open: true, childOuter: false },
      { id: 21, pId: 2, name: "can't be parent  2-1", dropInner: false },
      { id: 22, pId: 2, name: "can't be root 2-2", dropRoot: false },
      { id: 23, pId: 2, name: "try to drag me 2-3" },
      { id: 3, pId: 0, name: "can't add/sort my child 3", open: true, childOrder: false, dropInner: false },
      { id: 31, pId: 3, name: "can drag 3-1" },
      { id: 32, pId: 3, name: "can drag 3-2" },
      { id: 33, pId: 3, name: "can drag 3-3" }
    ];

    function dropPrev(treeId, nodes, targetNode) {
      var pNode = targetNode.getParentNode();
      if (pNode && pNode.dropInner === false) {
        return false;
      } else {
        for (var i = 0, l = curDragNodes.length; i < l; i++) {
          var curPNode = curDragNodes[i].getParentNode();
          if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
            return false;
          }
        }
      }
      return true;
    }

    function dropInner(treeId, nodes, targetNode) {
      if (targetNode && targetNode.dropInner === false) {
        return false;
      } else {
        for (var i = 0, l = curDragNodes.length; i < l; i++) {
          if (!targetNode && curDragNodes[i].dropRoot === false) {
            return false;
          } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
            return false;
          }
        }
      }
      return true;
    }

    function dropNext(treeId, nodes, targetNode) {
      var pNode = targetNode.getParentNode();
      if (pNode && pNode.dropInner === false) {
        return false;
      } else {
        for (var i = 0, l = curDragNodes.length; i < l; i++) {
          var curPNode = curDragNodes[i].getParentNode();
          if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
            return false;
          }
        }
      }
      return true;
    }

    var log, className = "dark", curDragNodes, autoExpandNode;

    function beforeDrag(treeId, treeNodes) {
      className = (className === "dark" ? "" : "dark");
      showLog("[ " + getTime() + " beforeDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes.");
      for (var i = 0, l = treeNodes.length; i < l; i++) {
        if (treeNodes[i].drag === false) {
          curDragNodes = null;
          return false;
        } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
          curDragNodes = null;
          return false;
        }
      }
      curDragNodes = treeNodes;
      return true;
    }

    function beforeDragOpen(treeId, treeNode) {
      autoExpandNode = treeNode;
      return true;
    }

    function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
      className = (className === "dark" ? "" : "dark");
      showLog("[ " + getTime() + " beforeDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
      showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is " + (isCopy == null ? "cancel" : isCopy ? "copy" : "move"));
      return true;
    }

    function onDrag(event, treeId, treeNodes) {
      className = (className === "dark" ? "" : "dark");
      showLog("[ " + getTime() + " onDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes.");
    }

    function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
      className = (className === "dark" ? "" : "dark");
      showLog("[ " + getTime() + " onDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
      showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is " + (isCopy == null ? "cancel" : isCopy ? "copy" : "move"));
    }

    function onExpand(event, treeId, treeNode) {
      if (treeNode === autoExpandNode) {
        className = (className === "dark" ? "" : "dark");
        showLog("[ " + getTime() + " onExpand ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name);
      }
    }

    function showLog(str) {
      if (!log) log = $("#log");
      log.append("<li class='" + className + "'>" + str + "</li>");
      if (log.children("li").length > 8) {
        log.get(0).removeChild(log.children("li")[0]);
      }
    }

    function getTime() {
      var now = new Date(),
        h = now.getHours(),
        m = now.getMinutes(),
        s = now.getSeconds(),
        ms = now.getMilliseconds();
      return (h + ":" + m + ":" + s + " " + ms);
    }

    function setTrigger() {
      var zTree = $.fn.zTree.getZTreeObj("treeDemo");
      zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
    }

    $(document).ready(function() {
      $.fn.zTree.init($("#treeDemo"), setting, zNodes);
      $("#callbackTrigger").bind("change", {}, setTrigger);
    });
    //-->
  </SCRIPT>
</HEAD>

<BODY>
<h1>Advanced Drag Node Operation</h1>
<h6>[ File Path: exedit/drag_super.html ]</h6>
<div class="content_wrap">
  <div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
  </div>
  <div class="right">
    <ul class="info">
      <li class="title">
        <h2>1, 'beforeDrag / onDrag'<br/>&nbsp;&nbsp;'beforeDrop / onDrop' callback function
        </h2>
        <ul class="list">
          <li>This Demo shows the configuration parameters to achieve a more flexible use of drag and drop rules, the operation can be compared with "Normal Drag Node Operation" example.</li>
          <li>This Demo default allows copy & move.</li>
          <li class="highlight_red">This Demo set 'setting.edit.drag.prev / inner / next' as 'function', you can dynamically set according to the node, which can not use beforeDrop callback function, the effect of drag and drop will be better.</li>
          <li>
            <p>
              <span class="highlight_red">After be familiar with these configurations, I think you can design a more flexible and rational rules!</span><br/>
              &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="callbackTrigger" checked/> 拖拽节点时自动展开父节点是否触发 callback<br/>
              drag / drop log:<br/>
              <ul id="log" class="log"></ul>
            </p>
          </li>
        </ul>
      </li>
      <li class="title">
        <h2>2, Explanation of setting</h2>
        <ul class="list">
          <li>Same as 'Normal Drag Node Operation'</li>
        </ul>
      </li>
      <li class="title">
        <h2>3, Explanation of treeNode</h2>
        <ul class="list">
          <li>Same as 'Normal Drag Node Operation'</li>
        </ul>
      </li>
    </ul>
  </div>
</div>
</BODY>
</HTML>